package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.util.List;
import javax.swing.JFrame;
//import main.Main;


/**
 * 
 * @author David Spruill
 */

@SuppressWarnings("serial")
public class GraphManager extends JFrame {
    /**
     * Constant for no key pressed.
     */

    private static final int FONT_SIZE = 12;
    private static final int BAR_X = 20;
    private static final int BAR_Y = 50;
    private static final int LINE_X = 20;
    private static final int LINE_Y = 65;
    private static final int CLEAR_X = 20;
    private static final int CLEAR_Y = 80;

 //   private Main myController;
    private Dimension mySize;
    private Axis myGraph;
    private List<Integer> myXData;
    // This list for myYData was List<Integer>, but because 
        //there is one string in the list for the labels, there is no data type...
    private List myYData;
    private int myOffset;
    private String myXLabel;
    private String myYLabel;

    /**
     * Displays the JFrame
     * 
     * @param startSize the size the frame starts out as.
     * @param data data from the model
     * @param inputOffset offset for the graph
     */
    public GraphManager (Dimension startSize, int inputOffset, List<Object> data) {
        mySize = startSize;
        usersSelect(startSize, inputOffset);
        setData(data);
        setBounds(0, 0, mySize.width, mySize.height);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Visualizer");
        setVisible(true);
    }

    /**
     * Paints the background of the graph
     * 
     * @param g the graphics object with which to draw
     */
    public void paint (Graphics g) {
        g.setColor(Color.white);
        g.clearRect(0, 0, mySize.width, mySize.height);

        drawControlLabels(g);

        // tell the graph how big it should be, and it's offset and labels
        if (myGraph != null) {
            // the head of the column is the ylabel
            if (myYData != null) {

                myGraph.setXLabel("ID");
                /**
                 * if (myXData != null) {
                 * myGraph.paint(g, myXData.subList(1, myXData.size()),
                 * myYData.subList(1, myYData.size()));
                 * }
                 */
                // else {
                myGraph.paint(g, myYData.subList(1, myYData.size()));
                // }
            }
        }
    }

    /**
     * This draws the controls to the screens.
     * 
     * @param g the graphics object to draw the labels
     */
    private void drawControlLabels (Graphics g) {
        Font controlsFont = new Font("TimesRoman", Font.PLAIN, FONT_SIZE);
        g.setFont(controlsFont);
        g.setColor(Color.black);
        g.drawString("h-histogram (the amount of friends for a range of users)", BAR_X, BAR_Y);
        g.drawString("f-frequency plot (the number of friends individual users have)", LINE_X,
                     LINE_Y);
        g.drawString("c-Clear and load new Data", CLEAR_X, CLEAR_Y);
    }

    /**
     * This is where the user will select their graph type.
     * 
     * @param size2 the size of the frame
     * @param inputOffset the difference between the edge of the frame and the
     *        graph itself
     */
    public void usersSelect (Dimension size2, int inputOffset) {

        myOffset = inputOffset;
        // Change this to Point instead of LineGraph and it will print a point
        // graph
        myGraph = new Histogram(mySize, myOffset);
        // This code has yet to be written,
        // but will take place here
        myGraph.setDefaults();
    }

    /**
     * Takes the input data and sets them to be used when called.
     * 
     * @param inputData theone column of information to be graphed
     */
    public void setData (List inputData) {
        myYData = inputData;
        myXData = null;
        //myYLabel = "Friend_Count";
        //Class cls = (myYData.get(0)).getClass();  
        //System.out.println("The type of the object is: " + cls.getName());
        //System.out.println(myYData.get(0));
        myYLabel = myYData.get(0).toString();
        myGraph.setYLabel(myYLabel);
    }

    protected Dimension getMySize () {
        return mySize;
    }

    protected void setMySize (Dimension size) {
        this.mySize = size;
    }

    protected Axis getMyGraph () {
        return myGraph;
    }

    protected void setMyGraph (Axis graph) {
        this.myGraph = graph;
    }

    protected List getMyXData () {
        return myXData;
    }

    protected void setMyXData (List xData) {
        this.myXData = xData;
    }

    protected List getMyYData () {
        return myYData;
    }

    protected void setMyYData (List yData) {
        this.myYData = yData;
    }

    protected int getMyOffset () {
        return myOffset;
    }

    protected void setMyOffset (int offset) {
        this.myOffset = offset;
    }

    protected String getMyXLabel () {
        return myXLabel;
    }

    protected void setMyXLabel (String xLabel) {
        this.myXLabel = xLabel;
    }

    protected String getMyYLabel () {
        return myYLabel;
    }

    protected void setMyYLabel (String yLabel) {
        this.myYLabel = yLabel;
    }

    /**
     * Takes the key pressed by the user and goes to the correct graph.
     * 
     * @param keyCode key pressed by user
     */
    public void toggleForce (int keyCode) {
        switch (keyCode) {
            case KeyEvent.VK_F:
                myGraph = new FrequencyPlot(mySize, myOffset);
                myGraph.setDefaults();
                myGraph.setXLabel(myXLabel);
                myGraph.setYLabel(myYLabel);
                //System.out.println("F");
                repaint();
                break;
            case KeyEvent.VK_H:
                myGraph = new Histogram(mySize, myOffset);
                myGraph.setDefaults();
                myGraph.setXLabel(myXLabel);
                myGraph.setYLabel(myYLabel);
                //System.out.println("H");
                repaint();
                break;
            default:
                break;
        }

    }

    /**
     * Takes the input data and sets them to be used when called.
     * 
     * @param inputDataX the x column of information
     * @param inputDataY the y column of information
     */
    /**
     * public void setData (List inputDataX, List inputDataY) {
     * myYData = inputDataX;
     * myXData = inputDataY;
     * myXLabel = myXData.get(0).toString();
     * myYLabel = myYData.get(0).toString();
     * myGraph.setXLabel(myXLabel);
     * myGraph.setYLabel(myYLabel);
     * }
     */
}